<?php

namespace Common\Model;

use Think\Model;

class UserFavoritesModel extends Model
{
    const TYPE_QUESTION = 1;

    /**
     * 统计关注数量
     *
     * @param string $objid
     * @param string $type
     * @return mixed
     */
    public function get_favorites_count($objid = '', $type = '1')
    {
        $map['objid'] = intval($objid);
        $map['type'] = intval($type);
        $count = $this->where($map)->count();
        return $count;
    }

    /**
     * 检测是否收藏
     *
     * @param string $uid
     * @param string $objid
     * @param string $type
     * @return bool
     */
    public function chenk_favorites($uid = '', $objid = '', $type = '')
    {
        $map['type'] = intval($type);
        $map['uid'] = intval($uid);
        $map['objid'] = intval($objid);
        $n = $this->where($map)->count();
        if ($n) {
            return true;
        }
        return false;
    }

    /**
     * 加入收藏
     *
     * @param string $uid
     * @param string $objid
     * @param string $type
     * @return bool
     */
    public function add_favorites($uid = '', $objid = '', $type = '')
    {
        $data['uid'] = intval($uid);
        $data['objid'] = intval($objid);
        $data['type'] = intval($type);
        $data['addtime'] = time();
        $this->add($data);

        # 收藏相关操作
        if ($type == 1) {
            D('UserTrack')->add_track($uid, $objid, 8); // 写入个人动态（问题收藏动态）
        }

        return true;
    }

    /**
     * 取消收藏
     *
     * @param string $uid
     * @param string $objid
     * @param string $type
     * @return bool
     */
    public function clear_favorites($uid = '', $objid = '', $type = '')
    {
        $map['uid'] = intval($uid);
        $map['objid'] = intval($objid);
        $map['type'] = intval($type);
        $this->where($map)->delete();
        return true;
    }
}